<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
include('clsketnoi.php');

function taomanhanvien()
{
    return taomatudong("select manv from tblnhanvien","manv", 2, 15);
}

function uploadfile()
{
        
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    //echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    
    if (file_exists("../upload/hinhnhanvien/" . $_FILES["file"]["name"]))
      {
      
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../upload/hinhnhanvien/" . $_FILES["file"]["name"]);
      
      }
    }
  }
else
  {
  
  }
}
//them du lieu
function themnhanvien()
{
    $ngaysinh = $_POST['ngaysinh'];
    $manv= mysql_real_escape_string($_POST["manv"]);
    $tendangnhap =$_POST["tendangnhap"];
    $matkhau = md5(trim($_POST["matkhau"])); 
    $hoten =$_POST["hoten"];
    $bangcap = $_POST['bangcap'];
    $chucvu = $_POST['chucvu'];
    $bophan = $_POST['bophan'];
    //lay ten hinh
    $hinh = $_FILES["file"]["name"];
    $quyen = $_POST['quyen'];
    $gioitinh = $_POST['gioitinh'];
    $diachi = $_POST['diachi'];
    $dienthoai = (int)$_POST['dienthoai'];
    $cmnd = (int)$_POST['cmnd'];

    mysql_query("insert into tblnhanvien (manv, tendangnhap, matkhau, hoten, bangcap, chucvu, bophan, quyen, diachi, ngaysinh, dienthoai, cmnd, hinh, gioitinh)".
                " values('$manv','$tendangnhap','$matkhau','$hoten','$bangcap','$chucvu','$bophan','$quyen','$diachi','$ngaysinh','$dienthoai','$cmnd','$hinh','$gioitinh')",dbketnoi());
    mysql_free_result();
}
//xoa nhan vien
function xoanhanvien()
{
    $manv= mysql_real_escape_string($_POST["manv"]);
    mysql_query("delete from tblnhanvien where manv='$manv'",dbketnoi());
    mysql_free_result();
}
//update nhan vien
function updatenhanvien()
{
    $ngaysinh = $_POST['ngaysinh'];
    $manv= mysql_real_escape_string($_POST["manv"]);
    $tendangnhap =$_POST["tendangnhap"];
    $matkhau =$_POST["matkhau"];
    $hoten =$_POST["hoten"];
    $bangcap = $_POST['bangcap'];
    $chucvu = $_POST['chucvu'];
    $bophan = $_POST['bophan'];
    //lay ten hinh
    $hinh = $_FILES["file"]["name"];
    $quyen = $_POST['quyen'];
    $gioitinh = $_POST['gioitinh'];
    $diachi = $_POST['diachi'];
    $dienthoai = $_POST['dienthoai'];
    $cmnd = (int)$_POST['cmnd'];
    
    mysql_query("update tblnhanvien set  tendangnhap='$tendangnhap', matkhau='$matkhau', hoten='$hoten', bangcap='$bangcap', chucvu='$chucvu',"."
     bophan='$bophan', quyen='$quyen', diachi='$diachi', ngaysinh='$ngaysinh', dienthoai='$dienthoai', cmnd='$cmnd', hinh='$hinh', gioitinh='$gioitinh' where manv='$manv'",dbketnoi());

    mysql_free_result();
}

// hien thi du lieu
function hienthi()
{
    $limit = 10; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT manv, tendangnhap, matkhau, hoten, bangcap, chucvu, bophan, quyen, diachi, ngaysinh, dienthoai, cmnd, hinh, gioitinh FROM tblnhanvien",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td ><a href='giaodiennhanvien.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo "<td><a href='giaodiennhanvien.php?manv=".$result['manv']."' >".$result['manv']."</a></td>";
            echo "<td>".$result['hoten']."</td>";
            echo "<td>".$result['gioitinh']."</td>";
            echo "<td>".$result['ngaysinh']."</td>";
            echo "<td>".$result['bophan']."</td>";
            echo "<td>".$result['chucvu']."</td>";
            echo "<td>".$result['cmnd']."</td>";
            echo "<td>".$result['dienthoai']."</td>";
            echo "<td>".$result['diachi']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("Mã NV ","Họ Tên","Giới Tính","Ngày Sinh", "Bộ Phận", "Chức Vụ", "CMND", "Điện Thoại", "Địa Chỉ");
    echo "<table style='border-color:black;'  cellspacing='1' width='900px' >";
    echo "<tr>";
    for($i=0; $i<=8; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}

function thongtinnhanvien()
{
    $manv = $_GET['manv'];
    $query = mysql_query("select hoten, hinh, diachi from tblnhanvien where manv='$manv'", dbketnoi());
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        
           echo "
            <table>
                <tr>
                    <td><img height='150' width='110' src='upload/hinhnhanvien/".$result['hinh']."'/></td>
                </tr>
                <tr>
                    <td>Tên Đầu Sách: ".$result['hoten']."</td>
                </tr>
                <tr>
                    <td>Giá Bìa: ".$result['diachi']." VNĐ</td>
                </tr>
            </table>
        
            </div>";
       
        $i++;
    }
}

?>
